var webpack = require('webpack');
var path = require('path');
var glob = require('glob');
var ROOT_PATH = path.resolve(__dirname, 'src/main/resources/static');
var SRC_PATH = path.resolve(ROOT_PATH, 'src');
var DIST_PATH = path.resolve(ROOT_PATH, 'dist');

var getEntries = function (SRC_PATH) {
    var all = glob.sync(path.resolve(SRC_PATH, '**/*.js'), {
        ignore: path.resolve(SRC_PATH, 'global/**/*.js')
    });
    const obj = {};
    for (var i = 0; i < all.length; i++) {
        var resolvePath = path.resolve(all[i]);
        var relativePath = path.relative(SRC_PATH, resolvePath);
        var dirname = path.dirname(relativePath);
        var basename = path.basename(resolvePath);
        var afterPath = path.join(DIST_PATH, dirname, basename);
        obj[afterPath] = resolvePath;
    }
    return obj;
}
var entries = getEntries(SRC_PATH);

module.exports = {
    //项目的文件夹 可以直接用文件夹名称 默认会找index.js 也可以确定是哪个文件名字
    entry: entries,
    //输出的文件名 合并以后的js会命名为bundle.js
    output: {
        filename: '[name]'
    },
    module: {
        preLoaders: [{
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'eslint-loader'
        }],
        loaders: [{
            test: require.resolve('jquery'), // 此loader配置项的目标是NPM中的jquery
            loader: 'expose?$!expose?jQuery', // 先把jQuery对象声明成为全局变量`jQuery`，再通过管道进一步又声明成为全局变量`$`
        }, {
            test: /\.jsx?$/,
            include: SRC_PATH,
            loader: 'babel',
            query: {
                presets: ['es2015']
            }
        }, {
            test: /\.(png|jpg|gif)$/,
            loader: 'url-loader?limit=8192' // 这里的 limit=8192 表示用 base64 编码 <= ８K 的图像
        }, {
            test: /\.css$/,
            loader: 'style!css'
        }, {
            test: /\.less$/,
            loader: 'style!css!less'
        }, ]
    },
    plugins: [
        
    ],
    devtool: 'source-map',
    eslint: {
        configFile: './.eslintrc'
    }
};